/**
 * Created by pactera on 16/12/28.
 */
module.exports = function ($rootScope,$scope,$state,$cookies, $window, findpasswordService,httpservice){


    $scope.phoneNum = '';
    $scope.phoneCode = '';
    $scope.emailNum = '';
    //身份验证验证码
    $scope.mode='6';
    var emailCode = '';
    $scope.errorShow = false;
    $scope.forgetPasswordConfirmMsg = "";
  var expireDate = new Date();
  expireDate.setDate(expireDate.getDate() + 1);

    //手机验证
    $scope.phoneVerify = function (){

        $('#phoneNum').removeClass('writeError');
        $('#phoneCode').removeClass('writeError');
        $('.error_phoneNum').fadeOut();
        $('.error_phoneCode').fadeOut();
        //判断账号
        if(checkPhone($scope.phoneNum)){
            //判断验证码
            if ($scope.phoneCode != ''){
                $rootScope.loadingShow = true;
                $rootScope.fadeShow = true;
                findpasswordService.phoneVerify($scope.phoneNum,$scope.phoneCode).then(function(data){

                    $rootScope.loadingShow = false;
                    $rootScope.fadeShow = false;
                    if (data.code == 1){
                      // $state.go('resetpassword',{phoneNum:$scope.phoneNum,emailNum:""});
                      $state.go('loginHeader.resetpassword',{phoneNum:$scope.phoneNum});
                      sessionStorage.setItem('phoneNum',$scope.phoneNum)

                    }else {
                        $('.error_phoneCode').fadeIn();
                        $('.error_phoneCode').children('p').html(data.msg);
                        $scope.getPicC();
                    }
                },function () {
                    $rootScope.loadingShow = false;
                    $rootScope.fadeShow = false;
                });
            }else{
                $('#phoneCode').addClass('writeError');
                $('.error_phoneCode').fadeIn();
            }
        }else {
            $('#phoneNum').addClass('writeError');
            $('.error_phoneNum').fadeIn();
        }
    };

    //邮箱验证
    $scope.emailVerify = function (){

        $('.error_emailNum').fadeOut();
        $('#emailNum').removeClass('writeError');

        //判断账号
        if(CheckMail($scope.emailNum)){
          if($scope.picCode!="" && $scope.picCode!=null && $scope.picCode!=undefined){
            $rootScope.loadingShow = true;
            $rootScope.fadeShow = true;
            findpasswordService.emailVerify($scope.emailNum,$scope.picCode).then(function(data){
              $rootScope.loadingShow = false;
              $rootScope.fadeShow = false;
              if (data.code == 1){
                // $state.go('resetpassword',{phoneNum:"",emailNum:$scope.emailNum});
                $scope.errorShow = true;
                $('.error_emailNum').children('p').html(data.msg);
                $('#forgetPasswordByEmailConfirm').modal('show');
                $state.go('loginHeader.emailright');
                //保存注册的邮箱,下个界面用
                var personageEmail = kbt.encrypt($scope.emailNum);
                $cookies.put('personageEmail',personageEmail,{
                  expires:expireDate
                });
              }else {
                $scope.getPicC();
                $scope.errorShow = true;
                $('.error_emailNum').fadeIn();
                $('.error_emailNum').children('p').html(data.msg);
              }
            },function (response) {
              $rootScope.loadingShow = false;
              $rootScope.fadeShow = false;
              console.error('emailVerify error', response.status, response.statusText);
            });
          }else{
            $('#picCode').addClass('writeError');
            return false;
          }


        }else {
            $('#emailNum').addClass('writeError');
            $('.error_emailNum').fadeIn();
        }
    };

  $scope.picUrl=  httpservice.getPicCodeUrl +"?time="+(new Date());
  //点击图片,更新验证码
  $scope.getPicC = function (){
    $scope.picUrl=  httpservice.getPicCodeUrl +"?time="+(new Date());
    $scope.picCode = "";
  };





  //点击获取验证码
    $scope.sendMesCod = function(){
        $('#phoneNum').removeClass('writeError');
        $('.error_phoneNum').fadeOut();

        //判断手机号
        if(checkPhone($scope.phoneNum)){
          if($scope.picCode!="" && $scope.picCode!=null && $scope.picCode!=undefined){

            findpasswordService.sendMesCod($scope.phoneNum,$scope.mode,$scope.picCode).then(function (data) {
              if (data.code == 1) {
                $scope.time();
                $('#phoneCode').removeClass('writeError');
                $('.error_phoneCode').fadeOut();
              }else if(data.code == 0){
              $scope.getPicC();
                $('.error_phoneCode p').html(data.msg);
                $('.error_phoneCode').show();
                return;
              }
            },function (data) {

            });
          }else {
            $('#phoneNum').addClass('writeError');
            $('.error_phoneNum').fadeIn();
          }
          }


    }

    // 去邮箱验证
    $scope.gotoEmailUrl = function(){
        $('#forgetPasswordByEmailConfirm').modal('hide');
        var emailUrl = "http://mail." + $scope.emailNum.substr($scope.emailNum.indexOf("@") + 1, $scope.emailNum.length);

        $window.location.href  = emailUrl;
    }

    var wait = 60;
    $scope.time = function (){
        if (wait == 0) {

            $('#getCode').attr("disabled",false);
            $('#getCode').val("获取验证码");
          $("#getCode").css({"color":"#0db3a6","border-color":"#0db3a6"});
            wait = 60;
        } else {
            $('#getCode').attr("disabled",true);
            $("#getCode").css({"color":"#666","border-color":"#666"});
            $('#getCode').val("重新发送（" + wait + "）");
            wait--;

            setTimeout(function () {
                $scope.time()
            }, 1000)
        }
    };

    //点击手机找回
    $('.findPhone').click(function(){
        $('#register-phon').css('display','block');
        $('#register-email').css('display','none');
    });

    //点击密码找回
    $('.findEmail').click(function(){
        $('#register-email').css('display','block');
        $('#register-phon').css('display','none');
    });

};
